{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Timeseries API Examples\n", "\n", "This notebook shows a few simple examples of retrieving data using the [Timeseries](https://docs.climateengine.com/docs/build/html/timeseries.html) family of endpoints. This group of endpoints are used to retrieve time series data for datasets in Climate Engine, including interannual and regression analysis. \n", "\n", "The CE API token is read as an environment variable named `CE_API_KEY`.\n", "\n", "Climate Engine \\\n", "October 2021" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from calendar import month_abbr\n", "import json\n", "import os\n", "import requests\n", "\n", "from folium import Map, GeoJson\n", "import folium\n", "from matplotlib import pyplot as plt\n", "import matplotlib.dates as mdates\n", "import numpy as np\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Set root URL for API requests\n", "root_url = 'https://geodata.dri.edu/'" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# Authentication info\n", "headers = {'Authorization': os.getenv('CE_API_KEY')}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Native Values - Landsat NDVI\n", "\n", "In this example, the /timeseries/native/custom_asset endpoint is used to collect averaged Landsat NDVI values for a group of four agricultural fields in the western US during the year 2020. The field polygons are retrieved from Google Earth Engine as a custom feature collection asset. All Landsat NDVI observations inside these polygons for the year 2020 are collected; multiple observations inside each field at the same time are averaged together. The NDVI trajectories for each field are plotted together, which shows clear cutting cycles for each field.\n", "\n", "Detailed documentation for the timeseries/native/custom_asset endpoint found [here](https://docs.climateengine.com/docs/build/html/timeseries.html#timeseries-native-custom-asset)." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# Endpoint\n", "endpoint = 'timeseries/native/custom_asset'" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# Extract field polygon coordinates from GeoJSON\n", "# These are the fields in the Earth Engine feature collection\n", "field_poly_json = r'example_fields.geojson'\n", "with open(field_poly_json, 'r') as f:\n", " field_polygon_dict = json.load(f)\n", "field_polygon_coords = field_polygon_dict['features'][0]['geometry']['coordinates']" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "